package com.fc.mall.portal.service.impl;

import com.fc.mall.mapper.SmsIntegrationSettingMapper;
import com.fc.mall.mapper.UmsIntegrationChangeHistoryMapper;
import com.fc.mall.model.SmsIntegrationSetting;
import com.fc.mall.model.UmsIntegrationChangeHistory;
import com.fc.mall.portal.dao.PortalMemberDao;
import com.fc.mall.portal.service.UmsIntegrationService;
import com.fc.mall.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description TODO
 * @Author Climb
 * @Date 2019/10/22
 **/
@Service
public class UmsIntegrationServiceImpl implements UmsIntegrationService {
    private final static Long DEFAULT_INTEGRATION_SETTING_ID = 1L;

    @Autowired
    private SmsIntegrationSettingMapper integrationSettingMapper;

    @Autowired
    private PortalMemberDao portalMemberDao;

    @Autowired
    private UmsIntegrationChangeHistoryMapper integrationChangeHistoryMapper;

    @Override
    public void getIntegration(Long memberId, Constants.SourceTypeEnum integrationWayEnum, Object ...params){
        switch (integrationWayEnum) {
            //签到获取积分, params[0]为int型连续签到天数
            case SIGN:
                handleSignIntegration(memberId, params);
                break;
            //购物获取积分，params[0]为int型消费金额（元）
            case TRADE:
                handleTradeIntegration(memberId, params);
                break;
            //分享获取积分，params[0]为int型分享次数
            case SHARE:
                handleShareIntegration(memberId, params);
                break;
            //邀请获取积分
            case INVITE:
                handleInviteIntegration(memberId);
                break;
            default:
                break;
        }
    }

    /**
     * 处理签到获取积分, params[0]为Integer型连续签到天数
     * @param memberId
     * @param params
     */
    private void handleSignIntegration(Long memberId, Object ...params)
    {
        SmsIntegrationSetting integrationSetting =
                integrationSettingMapper.selectByPrimaryKey(DEFAULT_INTEGRATION_SETTING_ID);
        //连续签到天数
        int continueSign = (int)params[0];
        if (Constants.SW_OPEN.equals(integrationSetting.getSignSw().intValue())) {
            int getIntegration = integrationSetting.getSignRate().intValue();
            //如果连续签到大于等于七天
            if (Constants.SW_OPEN.equals(integrationSetting.getContinueSignSw().intValue()) &&
                    continueSign >= Constants.CONTINUE_SIGN.intValue()){
                getIntegration =  getIntegration + integrationSetting.getContinueSignRate();
            }
            portalMemberDao.incIntegration(memberId,getIntegration);

            insertIntegrationChangeHistory(memberId, Constants.ChangeTypeEnum.ADD.getValue(),
                    getIntegration, Constants.SourceTypeEnum.SIGN);
        }
    }

    /**
     * 处理购物获取积分，params[0]为int型消费金额（元）
     * @param memberId
     * @param params
     */
    private void handleTradeIntegration(Long memberId, Object ...params)
    {
        SmsIntegrationSetting integrationSetting =
                integrationSettingMapper.selectByPrimaryKey(DEFAULT_INTEGRATION_SETTING_ID);

        if (Constants.SW_OPEN.equals(integrationSetting.getTradeSw().intValue())) {
            //消费金额
            int amount = (int) params[0];
            int getIntegration = amount * integrationSetting.getTradeExchangeRate() /Constants.TRAND_EXCHANGE_MONEY;
            portalMemberDao.incIntegration(memberId,getIntegration);

            insertIntegrationChangeHistory(memberId, Constants.ChangeTypeEnum.ADD.getValue(),
                    getIntegration, Constants.SourceTypeEnum.TRADE);
        }
    }

    /**
     * 处理分享获取积分，params[0]为Integer型分享次数
     * @param memberId
     * @param params
     */
    private void handleShareIntegration(Long memberId, Object ...params)
    {
        SmsIntegrationSetting integrationSetting =
                integrationSettingMapper.selectByPrimaryKey(DEFAULT_INTEGRATION_SETTING_ID);

        if (Constants.SW_OPEN.equals(integrationSetting.getShareSw().intValue())) {
            //分享次数
            int shareNum = (int)params[0];
            Integer getIntegration = shareNum * integrationSetting.getShareRate();
            portalMemberDao.incIntegration(memberId,getIntegration);

            insertIntegrationChangeHistory(memberId, Constants.ChangeTypeEnum.ADD.getValue(),
                    getIntegration, Constants.SourceTypeEnum.SHARE);
        }
    }

    /**
     * 处理邀请获取积分
     * @param memberId
     */
    private void handleInviteIntegration(Long memberId)
    {
        SmsIntegrationSetting integrationSetting =
                integrationSettingMapper.selectByPrimaryKey(DEFAULT_INTEGRATION_SETTING_ID);

        if (Constants.SW_OPEN.equals(integrationSetting.getInviteSw().intValue())) {
            int getIntegration = integrationSetting.getInviteRate().intValue();
            portalMemberDao.incIntegration(memberId,getIntegration);

            insertIntegrationChangeHistory(memberId, Constants.ChangeTypeEnum.ADD.getValue(),
                    getIntegration, Constants.SourceTypeEnum.INVITE);
        }
    }

    /**
     * 积分变化历史插入数据
     * @param memberId 会员ID
     * @param changeType 改变类型
     * @param changeCount 积分改变数量
     * @param sourceType 积分来源
     */
    private void insertIntegrationChangeHistory(Long memberId, Integer changeType,
                                                Integer changeCount, Constants.SourceTypeEnum sourceType){
        UmsIntegrationChangeHistory integrationChangeHistory = new UmsIntegrationChangeHistory();
        integrationChangeHistory.setMemberId(memberId);
        integrationChangeHistory.setCreateTime(new Date());
        integrationChangeHistory.setChangeType(changeType);
        integrationChangeHistory.setChangeCount(changeCount);
        integrationChangeHistory.setOperateNote(sourceType.getDescription());
        integrationChangeHistory.setSourceType(sourceType.getValue());
        integrationChangeHistoryMapper.insertSelective(integrationChangeHistory);
    }
}
